package com.scf.fruit.order.impl;

import com.scf.fruit.commodity.CommodityDto;
import com.scf.fruit.commodity.dao.CommodityMapper;
import com.scf.fruit.enums.OrderStatusEnum;
import com.scf.fruit.enums.StatusEnum;
import com.scf.fruit.order.OrderDetailInfoDto;
import com.scf.fruit.order.OrderInfoDto;
import com.scf.fruit.order.OrderInfoService;
import com.scf.fruit.order.dao.OrderDetailInfoMapper;
import com.scf.fruit.order.dao.OrderInfoMapper;
import com.scf.fruit.util.IDUtil;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;
import tk.mybatis.mapper.entity.Example;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author : 孙超飞
 * @title :
 * @description :
 * @copyright :
 * @date : 2018-05-18 17:07
 */
@Service
public class OrderInfoServiceImpl implements OrderInfoService {
    @Resource
    private OrderDetailInfoMapper orderDetailInfoMapper;
    @Resource
    private OrderInfoMapper orderInfoMapper;
    @Resource
    private CommodityMapper commodityMapper;

    @Transactional(rollbackFor = Exception.class)
    @Override
    public boolean saveOrder(OrderInfoDto dto, List<OrderDetailInfoDto> list) {
        dto.setCreateTime(new Date());
        dto.setDeleteFlag(StatusEnum.YES.getValue());
        dto.setId(IDUtil.getId());
        dto.setStatus(OrderStatusEnum.START.getValue());

        List<Long> collect = list.stream().map(x -> x.getCommodityId()).collect(Collectors.toList());
        
        Example example=new Example(OrderDetailInfoDto.class);
        Example.Criteria criteria=example.createCriteria();
        criteria.andIn("commodityId",collect);
        List<CommodityDto> commodityMappers = commodityMapper.selectByExample(example);

        Map<Long, CommodityDto> map = commodityMappers.stream().collect(Collectors.toMap(x -> x.getId(), y -> y));

        float total=0F;
        for(OrderDetailInfoDto x:list)
        {
            CommodityDto commodityDto = map.get(x.getCommodityId());
            if(null!=commodityDto){
                x.setCreateTime(new Date());
                x.setDeleteFlag(StatusEnum.YES.getValue());
                x.setOrderId(dto.getId());
                x.setId(IDUtil.getId());
                x.setTotalAmount(x.getNumber()*commodityDto.getPrice());
                total+=x.getTotalAmount();
            }
        }
        dto.setTotalAmount(total);

        return orderInfoMapper.insertSelective(dto)>0&&orderDetailInfoMapper.insertList(list)>0;
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public boolean deleteOrderDetail(Long id,Long userId) {
         OrderDetailInfoDto detailInfoDto = orderDetailInfoMapper.selectByPrimaryKey(id);
        OrderInfoDto orderInfoDto = orderInfoMapper.selectByPrimaryKey(detailInfoDto.getOrderId());
        if(null==detailInfoDto||null==orderInfoDto||!userId.equals(orderInfoDto.getUserId())){
            return false;
        }
        //当删除最后一个物品时
        OrderDetailInfoDto dto=new OrderDetailInfoDto();
        dto.setOrderId(detailInfoDto.getOrderId());
        List<OrderDetailInfoDto> select = orderDetailInfoMapper.select(dto);
        if(CollectionUtils.isEmpty(select)){
            orderInfoDto.setDeleteFlag(StatusEnum.NO.getValue());
        }

        Float v = orderInfoDto.getTotalAmount() - detailInfoDto.getTotalAmount();
        orderInfoDto.setTotalAmount(v);
        detailInfoDto.setDeleteFlag(StatusEnum.NO.getValue());

        return orderDetailInfoMapper.updateByPrimaryKeySelective(detailInfoDto)>0&&orderInfoMapper.updateByPrimaryKeySelective(orderInfoDto)>0;
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public boolean deleteOrder(Long id,Long userId) {
        OrderInfoDto orderInfoDto1 = orderInfoMapper.selectByPrimaryKey(id);
        if(null==orderInfoDto1||userId.equals(orderInfoDto1.getUserId())){
            return false;
        }
        orderInfoDto1.setDeleteFlag(StatusEnum.NO.getValue());
        OrderDetailInfoDto detailInfoDto = new OrderDetailInfoDto();
        detailInfoDto.setOrderId(id);
        List<OrderDetailInfoDto> select = orderDetailInfoMapper.select(detailInfoDto);
        select.stream().forEach(x->x.setDeleteFlag(StatusEnum.NO.getValue()));

        return  orderDetailInfoMapper.updateByPrimaryKeySelective(detailInfoDto)>0&&orderInfoMapper.updateByPrimaryKeySelective(orderInfoDto1)>0;
    }

    @Override
    public List<OrderInfoDto> queryOrder() {
        return orderInfoMapper.selectAll();
    }

    @Override
    public List<OrderDetailInfoDto> queryOrderDetailInfo(Long orderId) {
        OrderDetailInfoDto detailInfoDto=new OrderDetailInfoDto();
        detailInfoDto.setOrderId(orderId);
        return orderDetailInfoMapper.select(detailInfoDto);
    }
}
